package com.yingpai.fitness.dvp_2_0.main.activity.mine.shop_refund;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.bean.shop_refund.RefundListBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.round.RCRelativeLayout;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 退款/售后
 * Created by ASUS on 2018/11/28.
 */

public class RefundServiceListActivity extends CommonActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private QuickAdapter<RefundListBean.DataBean> quickAdapter;
    private Drawable drawableRefund;
    private Drawable drawableReturn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_fragment_order_list);
    }

    @Override
    protected void initView() {
        setTitle("退款/售后");

        drawableRefund = getResources().getDrawable(
                R.mipmap.icon_refund);
        drawableReturn = getResources().getDrawable(
                R.mipmap.icon_return_goods);

        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        quickAdapter = new QuickAdapter<RefundListBean.DataBean>(this, R.layout.item_refund_order_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, final RefundListBean.DataBean item) {
                helper.setText(R.id.title, item.getMerchandiseName());
                List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcip_value(), ShopDetailMcipChildBean.class);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < mcipList.size(); i++) {
                    if (i == mcipList.size() - 1) {
                        stringBuilder.append(mcipList.get(i).getPropertyValue());
                    } else {
                        stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                    }
                }
                helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                ImageView photo = helper.getView(R.id.photo);
                Glide.with(context)
                        .load(item.getPhotoUrls().split(",")[0])
                        .error(R.mipmap.default_small_bg)
                        .into(photo);
                //rc_layout.setClipBackground(true);
                rc_layout.setTopLeftRadius(6);
                rc_layout.setTopRightRadius(6);
                rc_layout.setBottomLeftRadius(6);
                rc_layout.setBottomRightRadius(6);

                TextView left_tv = helper.getView(R.id.left_tv);
                final TextView right_tv = helper.getView(R.id.right_tv);
                if (item.getRefundMark().equals(Constant.NUM_0 + "")) {
                    left_tv.setText("仅退款");
                    left_tv.setCompoundDrawablesWithIntrinsicBounds(drawableRefund, null, null, null);
                } else if (item.getRefundMark().equals(Constant.NUM_1 + "")) {
                    left_tv.setText("退货/退款");
                    left_tv.setCompoundDrawablesWithIntrinsicBounds(drawableReturn, null, null, null);
                }
                right_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("refundMark", item.getRefundMark());
                        bundle.putString("refundId", item.getRefundId());
                        startActivity(RefundServiceDetailActivity.class, bundle);
                    }
                });
            }
        };

        listview.setAdapter(quickAdapter);
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.GET_REFUND_LIST, null, Url.GET_REFUND_LIST, new Callback<RefundListBean>() {
            @Override
            public void onSuccess(RefundListBean refundListBean) {
                if (refundListBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {

                    if(null != refundListBean.getData() &&
                            refundListBean.getData().size() > 0){
                        empty.setVisibility(View.GONE);
                        quickAdapter.clear();
                        quickAdapter.addAll(refundListBean.getData());
                        quickAdapter.notifyDataSetChanged();
                    }else{
                        empty.setVisibility(View.VISIBLE);
                    }
                } else {
                    empty.setVisibility(View.VISIBLE);
                    showToast(refundListBean.getMessage() + "");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                empty.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    @OnClick({R.id.empty})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.empty:
                initData();
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }
}
